/*
 * Copyright 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.sychip.fhc.app.todo

import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavHostController
import androidx.navigation.navOptions
import com.sychip.fhc.app.drawer.nav.DrawerItem

/**
 * Screens used in [TodoDestinations]
 */
private object TodoScreens {
    const val START_SCREEN = "start"
    const val MAIN_TAB_SCREEN = "Main_Tab_ROUTE"
}

/**
 * Arguments used in [TodoDestinations] routes
 */
object TodoDestinationsArgs {
    const val USER_MESSAGE_ARG = "userMessage"
    const val TASK_ID_ARG = "taskId"
    const val TITLE_ARG = "title"
}

/**
 * Destinations used in the [com.fhc.app.MainActivity]
 */
object TodoDestinations {
    const val START_ROUTE = TodoScreens.START_SCREEN
    const val MAIN_TAB_ROUTE = TodoScreens.MAIN_TAB_SCREEN
 }

/**
 * Models the navigation actions in the app.
 */
class TodoNavigationActions(private val navController: NavHostController) {
    fun navigateToStart() {
        navController.popBackStack(
            route = TodoDestinations.START_ROUTE,
            inclusive = false,
            saveState = false)
    }
    fun navigateToMainTab() {
        navController.navigate(
            route = TodoScreens.MAIN_TAB_SCREEN,
            navOptions = navOptions {
                launchSingleTop = true   //保证返回栈栈顶只有一个目的地实例。类似Activity的singleTop启动模式
                popUpTo(navController.graph.findStartDestination().id) {   //将返回栈中popUpToId（默认不包括自己）之上的所有目的地弹出
                    inclusive = false   //与popUpToId配套使用，true表示popUpToId自己也要弹出返回栈
                    saveState = true    //与popUpToId配套使用，true表示保存所有弹出的目的地的状态
                }
            }
        )
    }

    fun navigateToDrawerMain() {
        navController.navigate(
            route = DrawerItem.TASKS.route,
            navOptions = navOptions {
                launchSingleTop = true   //保证返回栈栈顶只有一个目的地实例。类似Activity的singleTop启动模式
                popUpTo(navController.graph.findStartDestination().id) {   //将返回栈中popUpToId（默认不包括自己）之上的所有目的地弹出
                    inclusive = false   //与popUpToId配套使用，true表示popUpToId自己也要弹出返回栈
                    saveState = true    //与popUpToId配套使用，true表示保存所有弹出的目的地的状态
                }
            }
        )
    }

}
